package com.epam.rentcar.model;

import java.sql.SQLException;
import java.util.List;

import javax.naming.NamingException;

import org.apache.log4j.Logger;

import com.epam.rentcar.DAO.CarDAO;
import com.epam.rentcar.DAO.CarRequestDAO;
import com.epam.rentcar.DAO.DAOFactory;
import com.epam.rentcar.controller.actions.CloseRequestAction;
import com.epam.rentcar.transferobjects.Car;
import com.epam.rentcar.transferobjects.CarRequest;
import com.epam.rentcar.utils.dbconn.ConnectionFactory;

public class CarRent {

	private int userID;

	private static Logger log = Logger.getLogger(CloseRequestAction.class);

	private DAOFactory daoFactory = DAOFactory.getDAOFactory(DAOFactory.MySQL);
	private CarDAO carDAO = daoFactory.getCarDAO(ConnectionFactory.MySQL);
	private CarRequestDAO carRequestDAO = daoFactory.getCarRequestDAO(ConnectionFactory.MySQL);

	public CarRent(int userID){
		this.userID = userID;
	}

	public Car getCar(int carID) throws SQLException, NamingException{
		return carDAO.getCar(carID);
	}

	public boolean makeCarUnavaliable(int carID) throws SQLException, NamingException{		
		return carDAO.makeCarUnavaliable(carID);
	}

	public boolean makeCarAvaliable(int carID) throws SQLException, NamingException{	
		return carDAO.makeCarAvaliable(carID);
	}

	public boolean insertCar(String manufacturer, String model, String carNumber) throws SQLException, NamingException{
		return carDAO.addCar(manufacturer, model, carNumber);
	}

	public List<Car> getAvaliableCars() throws Exception {
		try {
			return carDAO.getAvaliableCars();
		} catch (SQLException | NamingException e) {
			log.error(e);
			throw new Exception(e);
		}
	}

	public boolean hasAvaliableCars() throws SQLException, NamingException{
		return !carDAO.getAvaliableCars().isEmpty();
	}


	public boolean addRequest(String name, String passport, int carID, int rentDuration) throws SQLException, NamingException{
		return carRequestDAO.addRequest(userID, name, passport, carID, rentDuration);
	}

	public List<CarRequest> getAllPayedCarRequests() throws Exception {
		try {
			return carRequestDAO.getAllPayedCarRequests();
		} catch (SQLException | NamingException e) {
			log.error(e);
			throw new Exception(e);
		}
	}

	public List<CarRequest> getAllCancelledRequests() throws Exception{
		try{
			return carRequestDAO.getAllCancelledRequests();
		} catch(SQLException | NamingException e) {
			log.error(e);
			throw new Exception(e);
		}
	}

	public List<CarRequest> getAllUserNotPayedCarRequests() throws Exception{
		try{
			return carRequestDAO.getAllUserNotPayedCarRequests(userID);
		} catch(SQLException | NamingException e){
			log.error(e);
			throw new Exception(e);
		}

	}

	public List<CarRequest> getAllRequests() throws SQLException, NamingException{
		return carRequestDAO.getAllRequests();
	}

	public List<CarRequest> getAllNotPayedCarRequests() throws Exception{
		try{
			return carRequestDAO.getAllNotPayedCarRequests();
		} catch(SQLException | NamingException e){
			log.error(e);
			throw new Exception(e);
		}	
	}

	public boolean cancellRequest(int requestID, String purpose) throws SQLException, NamingException{
		return carRequestDAO.cancellRequest(requestID, purpose);
	}

	public boolean approveRequest(int requestID) throws SQLException, NamingException{
		return carRequestDAO.approveRequest(requestID);
	}

	public boolean deleteRequest(int requestID) throws SQLException, NamingException{
		return carRequestDAO.deleteRequest(requestID);
	}

	public boolean setPaymentPurpose(int requestID, String purpose) throws SQLException, NamingException{
		return carRequestDAO.setPaymentPurpose(requestID, purpose);
	}

	public boolean payRequest(int requestID) throws SQLException, NamingException{
		return carRequestDAO.payRequest(requestID);
	}

	public boolean setRequestNotPayed(int requestID) throws SQLException, NamingException {
		return carRequestDAO.setRequestNotPayed(requestID);
	}

	public static void main(String[] args){

		//		CarRent carRent = new CarRent();

		//		for(Bill bill:carRent.getUserNotpayedBills()){
		//			System.out.println(bill);			
		//		}

	}
}
